package com.senarai.thedemit.ghost;

import org.anddev.andengine.entity.modifier.CubicBezierMoveModifier;
import org.anddev.andengine.entity.modifier.MoveModifier;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.anddev.andengine.opengl.texture.region.TextureRegion;
import org.anddev.andengine.entity.IEntity;
import org.anddev.andengine.util.modifier.ease.EaseLinear;
import org.anddev.andengine.util.modifier.ease.IEaseFunction;
import org.anddev.andengine.entity.modifier.CubicBezierMoveModifier;

import android.content.Context;
import android.util.Log;

import com.senarai.Global;
import com.senarai.Util;
import com.senarai.thedemit.Randomizer;

@SuppressWarnings("unused")
public class Ghost {
	//	Constants
	//	IDs
	private static final int rectangleGhostID = 0;
	//	names
	private static final String rectangleGhostName = "Rectangle Ghost";
	//	sprite path
	private static final String rectangleGhostPath = "Ghost/kuntilagi-tr.png";
	//	duration
	private static final float rectangleGhostDuration = 3.5f;
	//	minimal duration
	private static final float rectangleGhostMinDuration = 0f;
	//	maxPoint
	private static final int rectangleGhostMaxPoint = 1000; 
	
	//	Array
	//	Array of ID
	private static final int[] ghostIDs = new int[] {rectangleGhostID};
	//	Array of names
	private static final String[] ghostNames = new String[] {rectangleGhostName};
	//	Array of sprite path
	private static final String[] ghostPaths = new String[] {rectangleGhostPath};
	//	Array of duration
	private static final float[] ghostDurations = new float[]{rectangleGhostDuration};
	//	Array of min duration
	private static final float[] ghostMinDurations = new float[]{rectangleGhostMinDuration};
	//	Array of maxPoint
	private static final int[] ghostMaxPoint = new int[]{rectangleGhostMaxPoint};
	
	
	//	attribute
	/*
	 * sprite
	 * name
	 * score max
	 * sprite
	 * move path
	 * sound
	 * criteria
	 * probability
	 * id
	 */
	public int ID;
	public String name;
	public String path;
	public float maxDuration;
	public float minDuration;
	public int maxPoint;
	public Sprite sprite;
	
	/**
	 * constructor
	 */
	public Ghost(int ID){
		this.ID = ghostIDs[ID];
		this.name = ghostNames[ID];
		this.path = ghostPaths[ID];
		this.maxDuration= ghostDurations[ID];
		this.minDuration = ghostDurations[ID];
		this.maxPoint = ghostMaxPoint[ID];
	}
	
	public void move(int Camera_Width, int Camera_Height){
		if (this.sprite != null){
			float duration = Math.abs(Randomizer.randomizer(this.maxDuration)) + minDuration;
			float startX = Math.abs(Randomizer.randomizer(Camera_Width - sprite.getWidth()));
			float startY = Math.abs(Randomizer.randomizer(Camera_Height - sprite.getHeight()));
			float endX = Math.abs(Randomizer.randomizer(Camera_Width  - sprite.getWidth()));
			float endY = Math.abs(Randomizer.randomizer(Camera_Height - sprite.getHeight()));
			sprite.registerEntityModifier(new MoveModifier(duration, startX, endX, startY, endY));
			
			Util.println(Camera_Height + ", " + Camera_Width);
			Util.println(duration + ", " + startX + ", " + startY + ", " + endX + ", " + endY);
		}
	}
	
	public void moveVertical(int cameraWidth, int cameraHeight){
		if (sprite != null){
			float duration = Math.abs(Randomizer.randomizer(this.maxDuration)) + minDuration;
			float startX = Math.abs(Randomizer.randomizer(cameraWidth - sprite.getWidth()));
			float startY = Math.abs(Randomizer.randomizer(cameraHeight - sprite.getHeight()));
			float endY;
			if (isUpper(cameraHeight, (int)startY)){
				endY = cameraHeight;
			}else{
				endY = 0 - this.sprite.getHeight();
			}
			sprite.registerEntityModifier(new MoveModifier(duration, startX, startX, startY, endY));
		}
	}
	
	public void moveHorizontal(int cameraWidth, int cameraHeight){
		if (sprite != null){
			float duration = Math.abs(Randomizer.randomizer(this.maxDuration)) + minDuration;
			float startX = Math.abs(Randomizer.randomizer(cameraWidth - sprite.getWidth()));
			float startY = Math.abs(Randomizer.randomizer(cameraHeight - sprite.getHeight()));
			float endX;
			if(isLefter(cameraWidth, (int)startX)){
				endX = cameraWidth;
			}else{
				endX = 0 - this.sprite.getWidth();
			}
			float endY = startY;
			sprite.registerEntityModifier(new MoveModifier(duration, startX, endX, startY, startY));
			Log.d("pret", "duration : " + duration + ", startX " + startX + "endX : " + endX + "startY" + startY + "endY " + endY);
		}
	}
	private boolean isUpper(int cameraHeight, int ordinat){
		if (ordinat < cameraHeight/2) return true;
		else return false;
	}
	private boolean isLefter(int cameraWidth, int absis){
		if (absis < cameraWidth/2) return true;
		else return false;
	}
	/*
	 * get kuadran of a point, as in cartesian diagram 
	 */
	private int getKuadran(int cameraHeight, int cameraWidth, int ordinat, int absis){
		if (isUpper(cameraHeight, ordinat)){
			if (isLefter(cameraWidth, absis)) return 2;
			else return 1;
		}else{
			if (isLefter(cameraWidth, absis)) return 3;
			else return 4;
		}
	}
	/*
	 * get random absis
	 */
	private int getRandomAbsis(int kuadran, int cameraWidth){
		int absis = Randomizer.randomizer((int)(cameraWidth/2));
		if (kuadran == 1 || kuadran == 4){
			absis += cameraWidth/2; 
		}
		return absis;
	}
	/*
	 * get random ordinat
	 */
	private int getRandomOrdinat(int kuadran, int cameraHeight) {
		int ordinat = Randomizer.randomizer((int)(cameraHeight/2));
		if(kuadran == 3 || kuadran == 4){
			ordinat += cameraHeight/2;
		}
		return ordinat;
	}	
	public float childrenThiefMove(int cameraWidth, int cameraHeight){
		
		if (sprite != null){
			int side = Randomizer.randomizer(2);
			float[] ordinats = new float[4];
			float[] absisess = new float[4];
			if (side == 0){
				ordinats[0] = getRandomOrdinat(3, cameraHeight);
				ordinats[1] = getRandomOrdinat(2, cameraHeight);
				ordinats[2] = getRandomOrdinat(4, cameraHeight);
				ordinats[3] = 0 - sprite.getHeight();
				absisess[0] = getRandomAbsis(3, cameraWidth);
				absisess[1] = getRandomAbsis(2, cameraWidth);
				absisess[2] = getRandomAbsis(4, cameraWidth);
				absisess[3] = getRandomAbsis(1, cameraWidth);
			}else{
				ordinats[0] = getRandomOrdinat(4, cameraHeight);
				ordinats[1] = getRandomOrdinat(1, cameraHeight);
				ordinats[2] = getRandomOrdinat(3, cameraHeight);
				ordinats[3] = 0 - sprite.getHeight();
				absisess[0] = getRandomAbsis(4, cameraWidth);
				absisess[1] = getRandomAbsis(1, cameraWidth);
				absisess[2] = getRandomAbsis(3, cameraWidth);
				absisess[3] = getRandomAbsis(2, cameraWidth);
			}
			//	set duration
			float duration = Math.abs(Randomizer.randomizer(this.maxDuration)) + minDuration;
			//	move bezier
			sprite.registerEntityModifier(new CubicBezierMoveModifier(duration, absisess[0], ordinats[0],absisess[1], ordinats[1],
					absisess[2], ordinats[2], absisess[3], ordinats[3], EaseLinear.getInstance()));
			return (float)duration;
		}
		else return 0;
	}
	
	public Boolean isOutScreen() {
		if ((sprite.getX()<(0-sprite.getWidth())) || (sprite.getY()<(0-sprite.getHeight()))) {
			return true;
		} else {
			return false;
		}
	}
		
}
